package blockChain;

import java.util.ArrayList;

/**
 * @author WuYongheng
 * @date 2022/10/22
 * @description 测试类
 */
public class TestBlockChain {

    public static ArrayList<Block> blockchain = new ArrayList<Block>();
    // 设置挖矿困难度为5，会体现在哈希值，以5个0开头，困难度越大，计算花的时间越多
    public static int difficulty = 5;

    /**
     * 手动创建3个区块，将区块的内容以json的格式展示
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("Trying to Mine block 1... ");
        // 创世块，前一个区块的哈希值为 0
        addBlock(new Block("Hi im the first block", "0"));

        System.out.println("Trying to Mine block 2... ");
        addBlock(new Block("Yo im the second block", blockchain.get(blockchain.size() - 1).hash));

        System.out.println("Trying to Mine block 3... ");
        addBlock(new Block("Hey im the third block", blockchain.get(blockchain.size() - 1).hash));

        String blockchainJson = StringUtil.getJson(blockchain);
        System.out.println("\nThe block chain: ");
        System.out.println(blockchainJson);

    }

    /**
     * 创建新的区块
     * @param newBlock
     */
    private static void addBlock(Block newBlock) {
        newBlock.mineBlock(difficulty);
        blockchain.add(newBlock);
    }
}
